<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2019 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Membergroup.php 93514 2019-01-28 17:12:46 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_member_group
 * +----------------------------------------------------------------------
 */

namespace application\member\controller;

use application\admin\controller\base as auth;
use ticky\request;

class membergroup extends auth {

    // 首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('member_group')->where($search['sql'])->order('groupid desc')->newpage();
        foreach ($ret['items'] as $row) {
            $auts = '';
            if (strpos($row['authority'], '1') !== false)
                $auts = $auts . '发信息,';
            if (strpos($row['authority'], '2') !== false)
                $auts = $auts . '发评论,';
            if (strpos($row['authority'], '3') !== false)
                $auts = $auts . '发内容,';
            if (strpos($row['authority'], '4') !== false)
                $auts = $auts . '投稿免审核';
            if ($row['authority'] == '0')
                $auts = '';
            $row['authority'] = $auts;
            $data[] = $row;
        }
        $this->assign('page', $ret['page']);
        $this->assign('items', $data);
        $this->assign('search', $search['arr']);
        $this->display('membergroup/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('member_group')->insert($data);
            showmsg('添加成功', '/member/membergroup');
        } else {
            $this->assign('postUrl', '/member/membergroup/add');
            $this->assign('action', '添加');
            $this->display('membergroup/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('groupid', 0);
            db('member_group')->where(['groupid' => $id])->update($data);
            showmsg('修改成功', '/member/membergroup');
        } else {
            $groupid = request::get('groupid', 0);
            $item = db('member_group')->where(['groupid' => $groupid])->find();
            $this->assign('postUrl', '/member/membergroup/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->display('membergroup/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('member_group')->delete(['groupid' => $id]);
        showmsg('删除成功', '/member/membergroup');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('member_group')->delete(['groupid' => $id]);
            }
            showmsg('删除成功', '/member/membergroup');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';
        if ($groupid = trim($search['groupid'])) {
            $where .= "and groupid = '{$groupid}' ";
        }
        if ($name = trim($search['name'])) {
            $where .= "and name = '{$name}' ";
        }
        if ($experience = trim($search['experience'])) {
            $where .= "and experience = '{$experience}' ";
        }
        if ($icon = trim($search['icon'])) {
            $where .= "and icon = '{$icon}' ";
        }
        if ($authority = trim($search['authority'])) {
            $where .= "and authority = '{$authority}' ";
        }
        if ($description = trim($search['description'])) {
            $where .= "and description = '{$description}' ";
        }
        if ($is_system = trim($search['is_system'])) {
            $where .= "and is_system = '{$is_system}' ";
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['name'] = request::post('name', '');
        $arr['experience'] = request::post('experience', '');
        $arr['icon'] = request::post('icon', '');
        if (isset($_POST['authority'])) {
            $arr['authority'] = join(',', $_POST['authority']);
        } else {
            $arr['authority'] = '';
        }
        $arr['description'] = request::post('description', '');
        $arr['is_system'] = request::post('is_system', '');
        return $arr;
    }

}
